<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
    TR/html4/strict.dtd">
<HTML>
    <HEAD>
        <STYLE TYPE="text/css">
            <!--

            BODY { color: #000000; background-color: #f6f6f6; }
            -->
        </STYLE>

        <TITLE>Inzendopdracht 051R4</TITLE>
    </HEAD>
    <BODY>
        <FORM ACTION="051R4_engine.php" METHOD="GET">
            <DIV id="container" style="width:100%;">
                <DIV id="topspacing" style="height:100px;">
                    <BR><BR>
                </DIV>	
                <DIV id="leftspacing" style="width:10%;float:left;">
                    <BR>
                </DIV>
                <DIV id="messages" style="width:45%;float:left;">
                    <H2>Gastenboek</H2>
                    <P>Laat een bericht achter in het gastenboek, en vertel iedereen wat je van sport vindt.</P><BR>
                <?php
                require_once("./051R4_dbConnect.inc"); // database settings

                //Toon alle berichten. De laatste bovenaan.
                $qMessages = "SELECT naam, boodschap, datum, sport FROM gastenboek order by datum desc";
                $queryResult = mysqli_query($databaseConnection, $qMessages);
                
                if (!$queryResult) {
                    exit("Het is niet gelukt om de berichten op te halen uit de tabel.");
                }

                if (mysqli_num_rows($queryResult) == 0) {
                    print("<P>Het gastenboek is nog leeg.<BR>");
                    print("Plaats als eerste een bericht!</P>");
                    print("<hr>");
                } else {
                    print("<hr>");
                    while ($row = mysqli_fetch_array($queryResult, MYSQLI_ASSOC)) {
                        print('<DIV STYLE="text-align:right;">' . $row["datum"] . "</DIV>");
                        print('<DIV STYLE="text-align:left;font-size:1.5em;">' . $row["naam"] . "</DIV><BR>");
                        print(wordwrap(nl2br($row["boodschap"]), 75, "<BR>"));
                        print("<hr>");
                    }
                }
                mysqli_free_result($queryResult);

                //Selecteer de lijst met sporten uit de database.
                $qSportsList = "SELECT sport FROM sportlijst order by sport asc";
                $queryResult = mysqli_query($databaseConnection, $qSportsList);
                
                if ($queryResult) {
                    $sportsListHTML = "<select name=\"sport\">";
                    $sportsListHTML .= "<option value=\"empty\"></option>";
                    while ($row = mysqli_fetch_array($queryResult, MYSQLI_ASSOC)) {
                        $sportsListHTML .= "<option value=\"" . $row["sport"] . "\">" . $row["sport"] . "</option>";
                    }
                    $sportsListHTML .= "</select>";
                    mysqli_free_result($queryResult);
                } else {
                    $sportsListHTML = "Fout bij het ophalen van de sportlijst";
                }
                
                mysqli_close($databaseConnection);
                ?>
                </DIV>
                <DIV id="spacing" style="width:10%;float:left;">
                    <BR>
                </DIV>
                <DIV id="form" style="width:25%;float:left;">
                    <DIV id="labels" style="width:130px;float:left;">
                        <label for="name">Naam : </label><BR><BR>
                        <label for="sport">Sport : </label><BR><BR>
                        <label for="beoefenaar">Beoefenaar : </label><BR><BR>
                    </DIV>  
                    <DIV id="velden" style="width:130px;float:left;">
                        <INPUT TYPE="text" NAME="name"/><BR><BR>
                        <?php print($sportsListHTML); ?><BR><BR>
                        <input type="radio" name="practioner" value="fan"/>Fan<BR>
                        <input type="radio" name="practioner" value="sporter"/>Sporter<BR><BR>
                    </DIV>
                    <TEXTAREA NAME="message" ROWS="15" COLS="40"/></TEXTAREA><BR><BR>
                    <INPUT TYPE="submit" NAME="submit" VALUE="Verstuur"><BR>
                </DIV>
    </FORM>
</BODY>
</HTML>